﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KvoMvc.BusinessEntity;
using System.Web.Security;
using KvoMvc.BusinessContract;


namespace KvoMvc.BusinessComponent
{
    public class SecurityManager: ISecurityManager
    {
        /// <summary>
        /// Validate user.
        /// </summary>
        /// <param name="logOn"></param>
        /// <returns></returns>
        public bool IsValidUser(LogOn logOn)
        {
            bool result = Membership.ValidateUser(logOn.UserName, logOn.Password);
            return result;
        }

        /// <summary>
        /// Set authenticate cookie.
        /// </summary>
        /// <param name="logOn"></param>
        public void SetAuthCookie(string userName, bool isCreatePersistentCookie)
        {
            FormsAuthentication.SetAuthCookie(userName, isCreatePersistentCookie);
        }

        /// <summary>
        /// Sign out.
        /// </summary>
        public void SignOut()
        {
            FormsAuthentication.SignOut();
        }

        /// <summary>
        /// Create new user.
        /// </summary>
        /// <param name="register"></param>
        /// <returns></returns>
        public MembershipCreateStatus CreateUser(Register register)
        {
            MembershipCreateStatus result;
            try
            {
                Membership.CreateUser(register.UserName, register.Password, register.Email, "question", "answer", true, null, out result);
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
           
            return result;
        }
    }
}
